<template>
  <div class="page-container page-footer-btn">
    <van-cell-group>
      <van-row >
        <van-col span="24">
          <FortenScan
            ref="scan"
            propPlaceholder="请扫描SN标签"
            propErrorMsg="请扫描正确的SN标签"
            :propTypeArr="['sn']"
            @scanCall="handleScanOver"
          />
        </van-col>
       <!-- <van-col span="6">
          <van-button size="small" type="primary" @click="handleBatchAdjust">批量调整</van-button>
        </van-col>-->
      </van-row>

      <van-divider><span style="font-size:15px">库存信息</span></van-divider>
      <van-field label="耗材名称" :value="form.matrName" readonly />
      <van-field label="耗材品规" :value="form.matrSpec" readonly />
      <van-field label="包装单位" :value="form.packageUnit" readonly />
      <van-field label="包装数量" :value="form.packageQty" readonly />
      <van-field label="当前货位" :value="form.locationName" readonly />

      <van-row >
        <van-col span="18">
          <FortenScan
            ref="scanLocation"
            propPlaceholder="请扫描货位编码/识别码"
            propErrorMsg="请扫描货位编码/识别码"
            @scanCall="handleScanLocationOver"
            :noAutoFocus="true"
          />
        </van-col>
        <van-col span="6">
          <van-button size="small" type="primary" @click="openLocation">选择库位</van-button>
        </van-col>
      </van-row>
      <van-field label="仓库" :value="currentLocation.warehouseName" readonly />
      <van-field label="库位" :value="currentLocation.name" readonly />
      <van-field label="编码" :value="currentLocation.code" readonly />
      <van-field label="识别码" :value="currentLocation.idcode" readonly />
    </van-cell-group>

    <van-row type="flex" justify="space-around" class="footer-btn"  gutter="12"  >
      <van-col span="8">
        <van-button  type="primary" block @click="bindMatrLocation('stock', '确认调整该库存到所选库位么')" :disabled="!this.currentLocation || !this.currentLocation.id || !this.form.id">
          调整该库存
        </van-button>
      </van-col>
      <van-col span="8">
        <van-button type="danger" block @click="bindMatrLocation('matr', '确认调整该耗材所有库存到所选库位么')" :disabled="!this.currentLocation || !this.currentLocation.id || !this.form.id">
          调整所有库存
        </van-button>
      </van-col>
    </van-row>


    <FortenList ref="listItemLocation" :propAutoFetchData = 'false'
      :propIsRadio="true"
      :propFlagPopup="true"
      :propSearchFields="['name']"
      :propSearchTextAppend="'名称/代码'"
      propUrl="/base/location/pageForSelect"
      @change="changeLocation"
    >
      <template  #default="{item}"  >
        <van-row >
          <van-col span="4">
            <van-radio   :name="item"  />
          </van-col>
          <van-col span="4">名称：</van-col>
          <van-col span="16">{{item.name}}</van-col>
        </van-row>
        <van-row  >
          <van-col span="4">
          </van-col>
          <van-col span="4">代码：</van-col>
          <van-col span="16">{{item.code}}</van-col>
        </van-row>
      </template>
    </FortenList>
  </div>
</template>
<script>
import barcodeParse from '@/util/barcodeParse.js'

export default {
  name: '货位调整',
  data() {
    return {
      scan: '',
      scanLocation: '',
      form: {},
      currentLocation: {}
    }
  },
  props: {
    propFlowKey: String
  },
  components: {
  },
  methods: {
    //扫描
    handleScanOver(scanValue) {
      let codeType = barcodeParse.barCodeType(scanValue)
      if (codeType != 'SN' && codeType != 'RFID') {
        this.$notify.warning('请扫描有效的SN')
        return
      }

      let params = {
        isScan: 1,
        uniqueCode: scanValue
      }
      this.form = {}
      this.$ajax.get('/wm/stock/getStockByUniqueCode', {params}).then(resp => {
        if (resp.data) {
          this.form = resp.data
          this.$refs.scanLocation.focus()
        }
      })
      this.scan = ''
    },
    handleScanLocationOver(scanValue) { //添加新的默认货位
      let wrapper = this.$newWrapper().eq('code', scanValue).or('idcode', scanValue)
      let params = {
        isScan: 1,
        queryString: JSON.stringify(wrapper.queryConditions)
      }
      this.$ajax.get('/base/location/list', {params}).then(resp => {
        if (resp.data && resp.data.length > 0) {
          this.currentLocation = resp.data[0]
        } else {
          this.$notify.warning('不能识别的货位编码！')
          return
        }
        this.scanLocation = ''
      })
    },
    openLocation() {
      if (!this.form.id) {
        this.$notify.warning('请先扫描或选择耗材')
        this.$refs.scan.focus()
        return
      }
      let warehouseId = this.utils.getCurrentWarehouse().warehouseId
      let warehouseName = this.utils.getCurrentWarehouse().warehouseName
      let wrapper = this.$newWrapper().eq('warehouseId', warehouseId).ne('name', warehouseName)
      this.$refs.listItemLocation.setQueryWrapper(wrapper)
      this.$refs.listItemLocation.show()
    },
    //库位选择
    changeLocation(item) {
      this.currentLocation = item
    },
    async bindMatrLocation(type, msg) { //绑定新的货位
      const result = await this.$dialog.confirm({
        title: '调整确认',
        message: msg
      })
      if (!result) {
        return
      }

      let params = {
        location: this.currentLocation,
        'type': type,
        stocks: [this.form]
      }

      this.$ajax.post('/wm/transfer/adjustLocationByType', params).then(resp => {
        if (resp.code == 0) {
          this.$notify.success(resp.msg)
          this.$refs.scan.focus()
          this.form = {}
          this.currentLocation = {}
        }
      })
    },
    handleBatchAdjust() {
      this.$router.push({name: 'batchAdjust'})
    }
  },
  mounted() {
    this.$refs.scan.focus()
  }
}
</script>

<style lang="scss" scoped>
  .van-col {
    text-align: center;
  }
</style>
